﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
using AutoMapper;
using Illinois.Core.Interfaces;
using Illinois.Core.Services;
using Illinois.Infrastructure.Data;
using Illinois.UI.Models;
using Newtonsoft.Json;

namespace Illinois.UI.Controllers
{
  public class BurnDownController : BaseController
  {
    private readonly IBurnDownService _service;

    // todo: use CASTLE asap
    public BurnDownController()
      : this(new BurnDownService(new IllinoisContext()))
    {

    }

    public BurnDownController(IBurnDownService service)
    {
      _service = service;
    }

    public ActionResult Index()
    {
      var list = _service.GetAll(CurrentUser.Project.Id);
      // mapping
      Mapper.CreateMap<IBurnDown, BurnDownViewModel>();
      var models = Mapper.Map<IList<IBurnDown>, IList<BurnDownViewModel>>(list);
      // /mapping
      return View(models);
    }

    public ActionResult Details(int id)
    {
      var item = _service.GetById(id);
      // redirect to index if "wrong" projectId
      if (item.ProjectId != CurrentUser.Project.Id)
        return RedirectToAction("Index", "BurnDown");
      // mapping
      Mapper.CreateMap<IBurnDown, BurnDownViewModel>();
      var model = Mapper.Map<IBurnDown, BurnDownViewModel>(item);
      model.MakeLabels();
      // /mapping
      return View(model);
    }

    public ActionResult Delete(int id)
    {
      var result = _service.Delete(id);
      return RedirectToAction("Index");
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Create()
    {
      return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(string title, string start, string end, string points)
    {
      DateTime startOut;
      DateTime.TryParseExact(start, "dd/MM/yyyy", null, DateTimeStyles.None, out startOut);
      DateTime endOut;
      DateTime.TryParseExact(end, "dd/MM/yyyy", null, DateTimeStyles.None, out endOut);
      var result = _service.Add(title, startOut, endOut, Convert.ToInt16(points), CurrentUser.Project.Id);
      return RedirectToAction("Index");
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Edit(int id)
    {
      var item = _service.GetById(id);
      // mapping
      Mapper.CreateMap<IBurnDown, BurnDownViewModel>();
      var model = Mapper.Map<IBurnDown, BurnDownViewModel>(item);
      // /mapping
      return View(model);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, string title, string points)
    {
      var result = _service.Edit(id, title, Convert.ToInt16(points));
      return RedirectToAction("Index");
    }

    public string UpdateRemainings(int burnDownId, string order, string id, string points)
    {
      // add/update points
      var result = _service.SaveStoryPoints(burnDownId,
        order == string.Empty ? -1 : Convert.ToInt16(order),
        id == string.Empty ? -1 : Convert.ToInt16(id),
        points == string.Empty ? -1 : Convert.ToInt16(points));
      // redirect to Details action
      return JsonConvert.SerializeObject(new { redirectToUrl = Url.Action("Details", new { id = burnDownId }) });
    }
  }
}
